#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QIcon>
#include <QTimer>
#include "SkillModel.h"
#include "SolarSystemModel.h"
#include "MountainRecordModel.h"
#include "PlayerModel.h"
#include "HeightRecordModel.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    
    // 设置Qt Quick样式为Basic，减少样式警告
    QQuickStyle::setStyle("Basic");

    // 设置应用程序信息
    app.setApplicationName("儿童成长助手");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("LitterPutting");

    // 创建模型实例
SkillModel skillModel;
SolarSystemModel solarSystemModel;
MountainRecordModel mountainRecordModel;
PlayerModel playerModel;
HeightRecordModel heightRecordModel;

QObject::connect(&skillModel, &SkillModel::playerStatsUpdated,
                 &playerModel, [&](int totalPoints, int totalStars) {
    playerModel.setTotalPoints(totalPoints);
    playerModel.setTotalStars(totalStars);
});

QObject::connect(&skillModel, &SkillModel::levelMedalEarned,
                 &playerModel, [&](const QString& treeId, int skillId, const QString& skillName, const QString& medalIcon) {
    playerModel.addMedal(treeId, skillId, skillName, medalIcon);
});

QObject::connect(&skillModel, &SkillModel::levelMedalRevoked,
                 &playerModel, [&](const QString& treeId, int skillId) {
    playerModel.removeMedal(treeId, skillId);
});

QObject::connect(&skillModel, &SkillModel::levelMedalUpdated,
                 &playerModel, [&](const QString& treeId, int skillId, const QString& newName, const QString& medalIcon) {
    playerModel.updateMedalInfo(treeId, skillId, newName, medalIcon);
});

skillModel.refreshPlayerStats();

    // 连接PlayerModel的云同步配置变化到SkillModel和HeightRecordModel
    QObject::connect(&playerModel, &PlayerModel::cloudSyncEnabledChanged, [&]() {
        if (playerModel.cloudSyncEnabled()) {
            skillModel.setCloudSyncConfig(
                playerModel.giteeOwner(),
                playerModel.giteeRepo(),
                playerModel.giteeToken()
            );
            heightRecordModel.setCloudSyncConfig(
                playerModel.giteeOwner(),
                playerModel.giteeRepo(),
                playerModel.giteeToken()
            );
        }
    });
    
    // 启动时检查云同步配置，如果已配置则自动同步
    if (playerModel.cloudSyncEnabled()) {
        skillModel.setCloudSyncConfig(
            playerModel.giteeOwner(),
            playerModel.giteeRepo(),
            playerModel.giteeToken()
        );
        heightRecordModel.setCloudSyncConfig(
            playerModel.giteeOwner(),
            playerModel.giteeRepo(),
            playerModel.giteeToken()
        );
        
        // 启动时检查云端数据
        QTimer::singleShot(1000, [&]() {
            skillModel.checkAndSyncOnStartup();
            heightRecordModel.checkAndSyncOnStartup();
        });
    }

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("skillModel", &skillModel);
    engine.rootContext()->setContextProperty("solarSystemModel", &solarSystemModel);
    engine.rootContext()->setContextProperty("mountainRecordModel", &mountainRecordModel);
    engine.rootContext()->setContextProperty("playerModel", &playerModel);
    engine.rootContext()->setContextProperty("heightRecordModel", &heightRecordModel);

    // 加载 QML
    const QUrl url(QStringLiteral("qrc:/qml/AppMain.qml"));

    // 模型初始化完成

    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
                         if (!obj && url == objUrl) {
                             QCoreApplication::exit(-1);
                         }
                     }, Qt::QueuedConnection);
    
    engine.load(url);
    
    int result = app.exec();
    return result;
}
